#include <stdio.h>
#include <stdlib.h>
#include "camellia.h"
#include <time.h>
#include <inttypes.h>
int64_t timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)
{
	  return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) -
	             ((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
}
int main(int argc, char* argv[])
{
	struct timespec start, end;
	CamImage source, dest;
	camLoadPGM(&source, argv[1]);
	camAllocateImage(&dest, source.width, source.height, source.depth);
	CamArithmParams param;
	//omp_set_num_threads(4);
	time_t beg = time(NULL);
	int i;
	const int iterations = 100;
	int op = atoi(argv[3]);
	switch(op)
	{
		case 0:
			param.operation = CAM_ARITHM_SELECT;
			param.c1=atoi(argv[4]);
			param.c2=atoi(argv[5]);
			param.c3=atoi(argv[6]);
			break;
		case 1:
			param.operation=CAM_ARITHM_THRESHOLD;
			param.c1=atoi(argv[4]);
			param.c2=atoi(argv[5]);
			param.c3=atoi(argv[6]);
			break;
		case 2:
			param.operation=CAM_ARITHM_DOUBLE_THRESHOLD;
			param.c1=atoi(argv[4]);
			param.c2=atoi(argv[5]);
			param.c3=atoi(argv[6]);
			param.c4=atoi(argv[7]);
			break;
		case 3:
			param.operation= CAM_ARITHM_ABS;	
			break;
		case 4:
			param.operation = CAM_ARITHM_INVERSE;
			break;
		default:
			param.operation = CAM_ARITHM_INVERSE;
			break;
	}
	clock_gettime(CLOCK_MONOTONIC, &start);
	int accumulated=0;
	for(i = 0; i < iterations; i++)
		accumulated=camMonadicArithm(&source, &dest, &param);
	clock_gettime(CLOCK_MONOTONIC, &end);
	uint64_t timeElapsed = timespecDiff(&end, &start);
	printf("%llu seconds for %d iterations\n", (unsigned long long) timeElapsed, iterations);
	printf("accumulated: %d\n",accumulated);

	camSavePGM(&dest, argv[2]);


}
